(display "\n========================================\n")

; 区间 X (Cx-CxPx, Cx+CxPx)
; 区间 Y (Cy-CyPy, Cy+CyPy)
; 区间 Z = XY
; ((Cx-CxPx)*(Cy-CyPy), (Cx+CxPx)*(Cy+CyPy))
; ((CxCy*(1-(Px+Py)+PxPy)), (CxCy*(1+(Px+Py)+PxPy)))
; 因为 Px、Py为很小数字，所以 PxPy 可以忽略
; 所以：
; ((CxCy*(1-(Px+Py)+PxPy)), (CxCy*(1+(Px+Py)+PxPy)))，可化为：
; ((CxCy*(1-(Px+Py))), (CxCy*(1+(Px+Py))))
; 即百分比误差为 Px+Py
; 下面进行验证

(define (mul-interval x y)
    (let ((p1 (* (lower-bound x) (lower-bound y)))
          (p2 (* (lower-bound x) (upper-bound y)))
          (p3 (* (upper-bound x) (lower-bound y)))
          (p4 (* (upper-bound x) (upper-bound y))))
         (make-interval (min p1 p2 p3 p4)
                        (max p1 p2 p3 p4))))

(define (make-center-width c w)
    (make-interval (- c w) (+ c w)))

(define (center i)
    (/ (+ (lower-bound i) (upper-bound i)) 2.0))

(define (width i)
    (/ (- (upper-bound i) (lower-bound i)) 2.0))


(define (make-center-percent c p)
    (make-interval (- c (* c p)) (+ c (* c p))))

(define (percent i)
    (/ (width i) (center i)))

(define (make-interval a b) (cons a b))
(define (lower-bound z) (car z))
(define (upper-bound z) (cdr z))

(define x (make-center-percent 20 0.02))
(define y (make-center-percent 10 0.03))
(define c (mul-interval x y))
(display (* (percent c) 100))
(display "\n========================================\n")
